<%-- 
    Document   : viewProduct
    Created on : 02 8, 12, 6:23:12 PM
    Author     : pauld
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="Controller.ProductManager"%>
<%@page import="Controller.UserManager"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
    //get the user and name session variable
    String user = (String)session.getAttribute("user");
    String name = (String)session.getAttribute("name");
    
    //if the user is logged in check for session integrity
    if( (String)session.getAttribute("user") != null ){
        
        //check session integrity by comparing the hash of the session token
        String currSession = request.getRemoteAddr().concat( session.getId() );
        try{
            if( !UserManager.checkSessionIntegrity(currSession, session) ){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
        }catch(NoSuchAlgorithmException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(UnsupportedEncodingException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(Exception e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        
    }
    
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="css/960_24_col.css" />
        <link rel="stylesheet" href="css/reset.css" />
        <link rel="stylesheet" href="css/text.css" />
        <link rel="stylesheet" href="css/style.css" />
        
        <title>FooBar Online Bookstore | View Product</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
        <div id="header-wrap">
            <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="index.jsp"><img src="images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="main.jsp">Products</a>
                                <ul style="display: none">
                                    <li><a href="main.jsp?show=book">Books</a></li>
                                    <li><a href="main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="main.jsp?show=cd">CD</a></li>
                                    <li><a href="main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <%
                            //check the access previlage of the currently logged in user and show the appopriate menu
                            if( user != null && UserManager.isAdmin(user) == 0 && UserManager.isFinanceManager(user) == 0 && UserManager.isProductManager(user) == 0 ){
                                out.println("<li><a href='myProfile.jsp'>My Profile</a></li>");
                            }
                            else if(UserManager.isAdmin(user) == 1){
                                out.println("<li><a href='admin/adminPanel.jsp'>Admin Panel</a></li>");
                            }
                            else if(UserManager.isFinanceManager(user) == 1){
                                out.println("<li><a href='admin/financePanel.jsp'>Sales Panel</a></li>");
                            }
                            else if(UserManager.isProductManager(user) != 0){
                                out.println("<li><a href='admin/productPanel.jsp'>Product Panel</a></li>");
                            }
                            %>
                            <li><a href="about.jsp">About</a></li>
                            <li><a href="contact.jsp">Contact Us</a></li>
                            <%
                            //if the user is logged in show the username and logout button
                            if(user == null){
                                out.println("<li><a href='login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button'/>"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <%
                try{
                //proceed only to fetching the product if
                //the url variable is not empty
                if( !request.getParameter("id").equals("") ){
                
                //only store the product index if the user is logged in
                //since only logged in users are able to buy a product
                if( user != null ){
                    //store the index of the product to a session variable
                    //storing is done just in case the user decides to buy the product
                    session.setAttribute( "buy-product", request.getParameter("id") );
                }
                    
                out.println("<div id='product-view' class='grid_12'>");
                
                //get a certain product based on its index
                //then print the results
                ResultSet product = ProductManager.getProduct( request.getParameter("id") );
                
                if( product.next() ){
                    out.println("<h2>" + product.getString(2) + "</h2>"
                    + "<h3>Summary:</h3>"
                    + "<p>" + product.getString(3) + "</p><br />"
                    + "<p class='genre'><b>Genre: </b>" + product.getString(4) + "</p>"
                    + "<p class='type'><b>Type: </b>" + product.getString(5) + "</p>"
                    + "<p class='price'><b>Price: Php</b>" + product.getString(6) + "</p>");
                    //+ "<form action='Transaction' method='post' name='buyProduct'>"
                    //+ "<input type='hidden' name='buy-product' value='" + product.getString(8) + "' />");
                    
                    //only users that logged in can buy a product
                    //an admin/manager cannot buy a product
                    if( user != null && product.getInt(7) == 1 && 
                        ( UserManager.isAdmin(user) == 0 &&
                        UserManager.isFinanceManager(user) == 0 &&
                        UserManager.isProductManager(user) == 0 ) ){
                        
                        //out.println("<input type='submit' name='buy' value='Buy' class='submit-button' />");
                        out.println("<a href='transaction.jsp' class='submit-button'>Buy</a>");
                        
                    }
                    //a user cannot buy a product that is discontinued
                    else if( user != null && product.getInt(7) == 0 && 
                        ( UserManager.isAdmin(user) == 0 &&
                        UserManager.isFinanceManager(user) == 0 &&
                        UserManager.isProductManager(user) == 0 ) ){
                        out.println("<p>Discontinued</p>");
                    }
                    //a user that is not logged in cannot buy a product
                    else if( user == null ){
                        out.println("<p><a href='login.jsp'>Login</a> or <a href='register.jsp'>Register</a> to Buy Products</p>");
                    }
                                       
                    out.println("</form>");
                }
                else{
                    out.println("<div class='grid_12'>"
                                + "<h1>There is no product to view</h1>"
                                + "<a href='main.jsp'>Back</a>"
                                + "</div>");
                }
                
                %>
                </div>
                <div id="product-comments" class="grid_12 suffix_12">
                    <%
                    try{
                        if( !request.getParameter("id").equals("") ) {
                            out.println("<h4>Comments</h4>");
                        }
                    }
                    catch(NullPointerException e1){}
                    catch(Exception e1){}
                    
                    //get all the comments of a certain product based on its index
                    //then print out the results
                    ResultSet comment = ProductManager.getProductComments( request.getParameter("id") );
                    
                    out.println("<ul id='comment-list'>");
                    while( comment.next() ){
                        out.println("<li>"
                                + "<p class='comment'>" + comment.getString(3) + "</p>"
                                + "<p class='commenter'><b>" + comment.getString(1) + "</b></p>"
                                + "</li>");
                    }
                    out.println("</ul>");
                    
                    //only a user that is logged in and has bought
                    //the product is able to comment on the product
                    if ( user == null && 
                            !request.getParameter("id").equals("") ){
                        out.println("<p><a href='login.jsp'>Login</a> to leave a comment on this product.</p>");    //is user is not logged in
                    }
                    else if( user != null && 
                            UserManager.isBought( user, request.getParameter("id")) &&
                            !request.getParameter("id").equals("") ){
                        out.println("<form action='comment' method='post' id='comment-form' class='styled-form'>"
                                + "<textarea name='comment-body' rows='3' cols='50' /></textarea><br />"
                                + "<input type='hidden' name='product' value='" + request.getParameter("id") + "' />"   //if user is logged in and has bought the product
                                + "<input type='submit' name='submit-comment' value='Comment' class='submit-button' />"
                                + "</form>"); 
                    }
                    else if( user != null && 
                            !UserManager.isBought( user, request.getParameter("id")) &&
                            !request.getParameter("id").equals("") ){
                        out.println("<p>You can not leave a comment on products you have not bought.</p>");
                    }
                    
                    //close database connection
                    ProductManager.closeDB();
                    
                    out.println("</div>");
                    
                    }
                    else{
                        out.println("<div class='grid_12'>"
                                + "<h1>There is no product to view</h1>"
                                + "<a href='main.jsp'>Back</a>"
                                + "</div>");
                    }
    
                    }
                    catch(NullPointerException e1){
                        out.println("<div class='grid_12'>"
                                + "<h1>There is no product to view</h1>"
                                + "<a href='main.jsp'>Back</a>"
                                + "</div>");
                    }
                    %>
            </div>
        </div>
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
